package com.salon.server.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.salon.server.entity.Member;
import com.salon.server.entity.User;
import com.salon.server.model.R;
import com.salon.server.model.request.MemberRequest;
import com.salon.server.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/salon/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * page
     */
    @PostMapping("page")
    public R page(@RequestBody MemberRequest request) {

        return R.success(memberService.page(request, new LambdaQueryWrapper<Member>()
                .eq(StrUtil.isNotEmpty(request.getName()), Member::getName, request.getName())
                .eq(StrUtil.isNotEmpty(request.getPhone()), Member::getPhone, request.getPhone())
        ));
    }

    @PostMapping("/login")
    public R login(@RequestBody Member member) {

        return memberService.loginValid(member);
    }

    /**
     * add
     */
    @PostMapping("add")
    public R add(@RequestBody Member member) {
        return R.success(memberService.saveOrUpdate(member));
    }

    /**
     * 增加余额
     */
    @PostMapping("/addBalance")
    public R addBalance(@RequestBody Member member) {
        memberService.addBalance(member);
        return R.success();
    }

    @PostMapping("/update")
    public R update(@RequestBody Member member) {
        return R.success(memberService.saveOrUpdate(member));
    }
}
